﻿; ==============================
; 数据采集工具 - 安装脚本 (Inno Setup)
; 版本号由 precompile.bat 自动注入
; ==============================

#define MyAppName "DataCollector"
#define MyAppExeName "DataCollector.exe"
#define MyAppVersion "1.1.1"

[Setup]
AppName={#MyAppName}
AppVersion={#MyAppVersion}
DefaultDirName={userappdata}\{#MyAppName}
DefaultGroupName={#MyAppName}
OutputBaseFilename={#MyAppName}_安装程序_v{#MyAppVersion}
ArchitecturesAllowed=x64
ArchitecturesInstallIn64BitMode=x64
PrivilegesRequired=admin
Compression=lzma2
SolidCompression=yes
SetupIconFile=..\assets\app.ico
UninstallDisplayIcon={app}\{#MyAppExeName}
WizardStyle=modern
ShowLanguageDialog=no
CloseApplications=yes
CloseApplicationsFilter=数据采集工具.exe
RestartApplications=yes

[Files]
; ✅ 完整复制整个 PyInstaller 输出目录（含 _internal、python312.dll、资源等）
Source: "..\dist\{#MyAppName}\*"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs createallsubdirs
; VC++ 运行库（用于自动安装）
Source: ".\VC_redist.x64.exe"; DestDir: "{tmp}"; Flags: deleteafterinstall; Attribs: hidden

[Icons]
Name: "{autoprograms}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"
Name: "{autodesktop}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Tasks: desktopicon

[Tasks]
Name: desktopicon; Description: "创建桌面快捷方式"; GroupDescription: "附加任务:"; Flags: checkedonce

[Run]
Filename: "{app}\{#MyAppExeName}"; Description: "启动 {#MyAppName}"; Flags: postinstall nowait skipifdoesntexist

[Code]
function IsVcRedistInstalled: Boolean;
var
  Key1, Key2, Key3, Key4: Boolean;
begin
  Key1 := RegKeyExists(HKLM, 'SOFTWARE\Microsoft\VisualStudio\14.0\VC\Runtimes\x64');
  Key2 := RegKeyExists(HKLM, 'SOFTWARE\Microsoft\VisualStudio\15.0\VC\Runtimes\x64');
  Key3 := RegKeyExists(HKLM, 'SOFTWARE\Microsoft\VisualStudio\16.0\VC\Runtimes\x64');
  Key4 := RegKeyExists(HKLM, 'SOFTWARE\Microsoft\VisualStudio\17.0\VC\Runtimes\x64');
  Result := Key1 or Key2 or Key3 or Key4;
end;

procedure BeforeInstall;
var
  ResultCode: Integer;
begin
  if not IsVcRedistInstalled then
  begin
    if not FileExists(ExpandConstant('{tmp}\VC_redist.x64.exe')) then
    begin
      MsgBox('未找到 VC++ 运行库文件！', mbError, MB_OK);
      Abort;
    end;

    MsgBox('正在安装 Visual C++ 2015-2022 运行库（约 14MB），请稍候...', mbInformation, MB_OK);
    ExtractTemporaryFile('VC_redist.x64.exe');
    if not Exec(ExpandConstant('{tmp}\VC_redist.x64.exe'), '/install /quiet /norestart', '',
       SW_HIDE, ewWaitUntilTerminated, ResultCode) then
    begin
      MsgBox('VC++ 运行库安装失败！' + #13#10 +
             '请手动下载安装：' + #13#10 +
             'https://aka.ms/vs/17/release/vc_redist.x64.exe',
             mbError, MB_OK);
    end;
  end;
end;
